package com.ezjie.ielts.module_read;

import java.util.ArrayList;
import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.ezjie.ielts.R;
import com.ezjie.ielts.constant.Constants;
import com.ezjie.ielts.core.base.BaseFragmentActivity;
import com.ezjie.ielts.core.http.HttpRequestAbstractCallBack;
import com.ezjie.ielts.db.UserInfoDB;
import com.ezjie.ielts.model.ReadClassifyInfo;
import com.ezjie.ielts.module_read.adapter.ReadClassisyAdapter;
import com.ezjie.ielts.module_word.WordPracticeActivity;
import com.ezjie.ielts.task.ReadClassifyTask;
import com.ezjie.ielts.util.AppUtil;
import com.ezjie.ielts.util.LogUtils;
import com.ezjie.ielts.util.UmengPages;
import com.ezjie.ielts.widget.TitleTextView;
import com.ezjie.login.LoginActivity;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.umeng.analytics.MobclickAgent;

@ContentView(R.layout.activity_read_classisy)
public class ReadClassifyActivity extends BaseFragmentActivity {

	@ViewInject(R.id.iv_topbar_back)
	private ImageView iv_topbar_back;
	@ViewInject(R.id.iv_topbar_right)
	private ImageView iv_topbar_right;
	@ViewInject(R.id.tv_topbar_title)
	private TitleTextView tv_topbar_title;
	@ViewInject(R.id.gv_subject_type_grideview)
	private GridView gridView;

	private Context mContext;
	private ReadClassisyAdapter adapter;
	private List<String> lists = new ArrayList<String>();

	private ReadClassifyTask mReadClassifyTask;

	private UserInfoDB userInfoDB;

	private RefreshClassfiyDataReceiver mRefreshClassfiyDataReceiver;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);

		mReadClassifyTask = new ReadClassifyTask(this);
		userInfoDB = new UserInfoDB(this);

		mContext = this;
		init();
	}

	private void init() {

		// mRefreshClassfiyDataReceiver = new RefreshClassfiyDataReceiver();
		// registerReceiver(mRefreshClassfiyDataReceiver, new
		// IntentFilter(Constants.REFRESH_CLASSFIY_DATA_ACTION));
//		iv_topbar_right.setVisibility(View.VISIBLE);
//		iv_topbar_right.setOnClickListener(new OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//				startActivity(new Intent(mContext, WordPracticeActivity.class));
//			}
//		});

		tv_topbar_title.setText(R.string.classify_practice_title);
		iv_topbar_back.setOnClickListener(this);
		adapter = new ReadClassisyAdapter(mContext);
		gridView.setAdapter(adapter);

		gridView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// 友盟统计
				MobclickAgent.onEvent(mContext, "readType_listItemClick");
				if (userInfoDB.queryLoginUser().islogin == 0) {
					startActivity(new Intent(mContext, LoginActivity.class));
					return;
				}
				Intent intent = new Intent(ReadClassifyActivity.this, ReadAllPracticeActivity.class);
				intent.putExtra(Constants.READ_TYPE, adapter.getItem(position).en);
				intent.putExtra(Constants.READ_FROM, Constants.READ_FROM_VALUE);
				intent.putExtra(Constants.READ_POSITION, Integer.valueOf(adapter.getItem(position).position));
				intent.putExtra(Constants.READ_TOTAL, Integer.valueOf(adapter.getItem(position).total));
				startActivity(intent);
			}
		});

	}

	@Override
	protected void onResume() {
		super.onResume();

		MobclickAgent.onPageStart(UmengPages.PAGE_READ_CLASSIFY);

		loadData();
	}

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPageEnd(UmengPages.PAGE_READ_CLASSIFY);
	}

	private void loadData() {

		mReadClassifyTask.getReadClassifyInfo(new HttpRequestAbstractCallBack(mContext,false) {
			@Override
			public void onStartCallBack() {
				super.onStartCallBack();
				AppUtil.showProgressDialog(mContext);
			}

			@Override
			public void onSuccessCallBack(String result) {
				super.onSuccessCallBack(result);
				LogUtils.d(result);
				AppUtil.dismissProgressDialog();
				try {
					ReadClassifyInfo info = JSON.parseObject(result, ReadClassifyInfo.class);
					adapter.setDatas(info.getData());
				} catch (Exception e) {
					LogUtils.exception(e);
				}
			}

			@Override
			public void onFailureCallBack(HttpException error, String msg) {
				super.onFailureCallBack(error, msg);
				LogUtils.e(msg);
				AppUtil.dismissProgressDialog();
				AppUtil.showToast(ReadClassifyActivity.this, R.string.load_net_data_failure);
			}
		});
	}

	@Override
	public void onClickEvent(View view) {
		switch (view.getId()) {
		case R.id.iv_topbar_back:
			finish();
			break;
		default:
			break;
		}
	}

	@Override
	public void onBackPressed() {
		finish();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (null != mRefreshClassfiyDataReceiver) {
			unregisterReceiver(mRefreshClassfiyDataReceiver);
		}
	}

	private class RefreshClassfiyDataReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equals(Constants.REFRESH_CLASSFIY_DATA_ACTION)) {
				loadData();
			}
		}

	}

}
